package com.huntech.log.controller;

import com.github.pagehelper.PageInfo;
import com.huntech.common.constant.RegularFormatConstant;
import com.huntech.common.domain.Response;
import com.huntech.log.domain.LogRecord;
import com.huntech.log.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 日志操作
 */
@Slf4j
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    /**
     * 批量插入日志
     */
    @PostMapping("insertBatch")
    public Response<Object> insertBatch(@RequestParam("file") MultipartFile logFile) {
        log.info("收到日志文件");
        logService.insertBatch(logFile);
        return Response.ok(null, "成功");
    }

    /**
     * 根据tag，分页查询时间段日志
     */
    @GetMapping("/getLog")
    public Response<PageInfo<LogRecord>>  getLog(
            @RequestParam @DateTimeFormat(pattern = RegularFormatConstant.TS_FORMAT_LOG) String startTime,
            @RequestParam @DateTimeFormat(pattern = RegularFormatConstant.TS_FORMAT_LOG) String endTime,
            @RequestParam(required = false) String logLevel,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ){
        PageInfo<LogRecord> pageInfo = logService.select(startTime,endTime,logLevel,pageNum,pageSize);
        return Response.ok(pageInfo,"查询成功！");
    }
}
